<?php
namespace app\modules\admin\controllers;

use app\controllers\UploadControllerTrait;
use app\models\Ad;
use app\models\AdLocation;
use app\models\ManagerLog;
use Yii;
use yii\data\Pagination;
use yii\helpers\FileHelper;
use yii\helpers\Url;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;

/**
 * 广告管理
 *
 * Class AdController
 * @package app\modules\admin\controllers
 */
class AdController extends BaseController
{
    /**
     * 广告列表
     * @throws ForbiddenHttpException
     * @return string
     */
    public function actionList()
    {
        if (!$this->manager->can('ad/list')) {
            throw new ForbiddenHttpException('没有权限');
        }
        $query = Ad::find()->where(['status'=>[Ad::STATUS_ACTIVE, Ad::STATUS_STOPED]]);
        $query->andFilterWhere(['lid'=>Yii::$app->request->get('search_lid')]);
        $query->andFilterWhere(['like', 'name', Yii::$app->request->get('search_name')]);
        $pagination = new Pagination(['totalCount'=>$query->count()]);
        $model_list = $query->orderBy('id DESC')->offset($pagination->offset)->limit($pagination->limit)->all();
        return $this->render('list', [
            'model_list'=>$model_list,
            'pagination'=>$pagination
        ]);
    }

    /**
     * 文件上传AJAX接口
     * @see \app\controllers\UploadControllerTrait
     */
    use UploadControllerTrait;

    /**
     * 添加/修改广告
     * @throws ForbiddenHttpException
     * @throws NotFoundHttpException
     * @return string
     */
    public function actionEdit()
    {
        if (!$this->manager->can('ad/edit')) {
            throw new ForbiddenHttpException('没有权限');
        }
        $id = Yii::$app->request->get('id');
        if ($id > 0) {
            $model = Ad::findOne($id);
            if (empty($model)) {
                throw new NotFoundHttpException('没有找到广告信息。');
            }
        } else {
            $model = new Ad();
            $model->lid = Yii::$app->request->get('lid');
        }
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            // 图片自动缩放
            if ($model->location->type == AdLocation::TYPE_IMAGE) {
                $location = $model->location;
                $width  = $location->width;
                $height = $location->height;
                if ($width > 0 && $height > 0) {
                    /* @var Yii::$app->image \yii\image\ImageDriver */
                    /* @var $image \yii\image\drivers\Image */
                    $image = Yii::$app->image->load(Yii::$app->params['upload_path'] . $model->img->url);
                    $image->resize($width, $height)->save();
                }
            }
            ManagerLog::info($this->manager->id, '', '保存广告：' . print_r($model->attributes, true));
            Yii::$app->session->addFlash('success', '数据已保存。');
            Yii::$app->session->setFlash('redirect', json_encode([
                'url'=>Url::to(['/admin/ad/list']),
                'txt'=>'广告列表'
            ]));
        }
        return $this->render('edit', [
            'model'=>$model
        ]);
    }

    /**
     * 删除广告AJAX接口
     * @throws ForbiddenHttpException
     * @throws NotFoundHttpException
     * @return array
     */
    public function actionDelete()
    {
        if (!$this->manager->can('ad/edit')) {
            throw new ForbiddenHttpException('没有权限。');
        }
        $id = Yii::$app->request->get('id');
        $model = Ad::findOne($id);
        if (empty($model)) {
            throw new NotFoundHttpException('没有找到广告信息。');
        }
        $model->status = Ad::STATUS_DELETED;
        $model->save();
        ManagerLog::info($this->manager->id, '', '删除广告：' . print_r($model->attributes, true));
        return [
            'result'=>'success'
        ];
    }

    /**
     * 设置广告状态AJAX接口
     * @throws ForbiddenHttpException
     * @throws NotFoundHttpException
     * @return array
     */
    public function actionStatus()
    {
        if (!$this->manager->can('ad/edit')) {
            throw new ForbiddenHttpException('没有权限。');
        }
        $id = Yii::$app->request->get('id');
        $model = Ad::findOne($id);
        if (empty($model)) {
            throw new NotFoundHttpException('没有找到广告信息。');
        }
        $model->status = [Ad::STATUS_ACTIVE=>Ad::STATUS_STOPED, Ad::STATUS_STOPED=>Ad::STATUS_ACTIVE][$model->status];
        if ($model->save()) {
            return [
                'result'=>'success'
            ];
        }
        return [
            'result'=>'failure',
            'message'=>'无法保存广告信息。',
            'errors'=>$model->errors
        ];
    }

    /**
     * 广告位置
     * @throws ForbiddenHttpException
     * @return string
     */
    public function actionLocation()
    {
        if (!$this->manager->can('ad/location')) {
            throw new ForbiddenHttpException('没有权限');
        }
        $query = AdLocation::find();
        $query->andFilterWhere(['like', 'name', Yii::$app->request->get('search_name')]);
        $pagination = new Pagination(['totalCount'=>$query->count()]);
        $model_list = $query->orderBy('id DESC')->offset($pagination->offset)->limit($pagination->limit)->all();
        return $this->render('location', [
            'model_list'=>$model_list,
            'pagination'=>$pagination
        ]);
    }

    /**
     * 添加/修改广告位置
     * @throws ForbiddenHttpException
     * @return string
     */
    public function actionEditLocation()
    {
        if (!$this->manager->can('ad/location')) {
            throw new ForbiddenHttpException('没有权限');
        }
        $id = Yii::$app->request->get('id');
        if ($id > 0) {
            $model = AdLocation::findOne($id);
        } else {
            $model = new AdLocation();
        }
        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
            // 生成模板文件
            if (!empty($model->code)) {
                $path = Yii::getAlias('@webroot/assets/ad');
                if (!file_exists($path)) {
                    if (!FileHelper::createDirectory($path)) {
                        Yii::$app->session->addFlash('error', '无法创建广告模板目录：' . $path);
                    }
                }
                $file = $path . '/' . $model->id . '.tpl';
                $file_saved = file_put_contents($file, $model->code);
                if (!$file_saved) {
                    Yii::$app->session->addFlash('error', '无法保存广告模板文件：' . $file);
                }
            }
            ManagerLog::info($this->manager->id, '', '保存广告位：' . print_r($model->attributes, true));
            Yii::$app->session->addFlash('success', '数据已保存。');
            Yii::$app->session->setFlash('redirect', json_encode([
                'url'=>Url::to(['/admin/ad/location']),
                'txt'=>'广告位置'
            ]));
        }
        return $this->render('location_edit', [
            'model'=>$model
        ]);
    }
}
